package classes.view.component
{
	import classes.controller.DAOSendJS;
	import classes.controller.ESendJS;
	import classes.model.VOIsLocal;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class SinglegameNotice extends Sprite
	{
		private var _box:Array;
		private var _subject:Array;
		private var _subjectCont:Sprite;
		public function SinglegameNotice()
		{
			setLayOut();
			defaultSetting();
		}
		
		public function set notice_switch($switch:Boolean):void
		{
			if($switch){
				this.visible = true;
				init_data();
			}else{
				this.visible = false;
				remove_data();
			}
			
		}
		
		private function init_data():void
		{
			if(VOIsLocal.getLocalInstance().isLocal == "FILE"){
				//test.
				var loader:URLLoader = new URLLoader();
				loader.addEventListener( Event.COMPLETE,	onDataLoaded);
				try {
					loader.load( new URLRequest('sample/singleroominfo.json'));
				}catch ( error:Error ) {
					trace( "Unable to load requested document." );
				}
			}else{
				DAOSendJS.getLoadInstance().addEventListener(ESendJS.EVENT_ROOMINFO,	onRoomInfoLoadcomplete);	
			}
			
		}
		
		private function onDataLoaded(e:Event):void
		{
			
			var jsonParsed:*  = JSON.parse(e.target.data);
			var obj:Object 	= jsonParsed.data[0];
			//trace(obj.data[0].profileimage);
			dataSetting = obj;
		}
		
		private function onRoomInfoLoadcomplete(e:ESendJS):void
		{
			var obj:Object 	= e._obj;
			dataSetting = obj;
		}
		
		private function set dataSetting($obj:Object):void
		{
			resetSubject();
			if(_subject==null)	_subject		= new Array();
			
			for(var i:int = 0 ; i < $obj.subject.length ; i++){
				_subject[i] = new NoticeSubjectObj();
				_subject[i].x = 600 + (-44*i);
				_subject[i].y = 5;
				_subject[i].subjectTitle = $obj.subject[i].sb_name;
				this._subjectCont.addChild(_subject[i]);
			}
			
			_box[2].x = _subject[_subject.length-1].x - 22;
			_box[1].width = _box[2].x - 30;
		}
		
		private function remove_data():void
		{
			
		}
		private function resetSubject():void
		{
			if(_subject.length > 0 ){
				while (this._subjectCont.numChildren > 0) {
					this._subjectCont.removeChildAt(0);
				}
				_subject.splice(0, _subject.length-1);
			}
			_subject = null;
		}
		
		private function setLayOut():void
		{
			if(_box==null)		_box 			= new Array();
			
			_box[0] = this.getChildByName('box_0')	as Sprite;
			_box[1] = this.getChildByName('box_1')	as Sprite;
			_box[2] = this.getChildByName('box_2')	as Sprite;
			
			if(_subject==null)	_subject		= new Array();
			
			_subjectCont = this.getChildByName('subjectcont_mc')	as Sprite;
			
		}
		private function defaultSetting():void
		{
			this.visible = false;
		}
	}
}